package com.java2007.hotel.service.impl;

import com.java2007.hotel.constant.Constant;
import com.java2007.hotel.dao.FoodDao;
import com.java2007.hotel.dao.FoodTypeDao;
import com.java2007.hotel.factory.BeanFactory;
import com.java2007.hotel.pojo.Food;
import com.java2007.hotel.pojo.FoodType;
import com.java2007.hotel.service.FoodService;
import com.java2007.hotel.service.FoodTypeService;
import com.java2007.hotel.utils.JdbcUtils;
import org.junit.Test;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * 菜系service实现类
 * @author csl
 * @version V1.0
 * @date 2021/01/22
 **/
public class FoodTypeServiceImpl implements FoodTypeService {
    private FoodTypeDao foodTypeDao = (FoodTypeDao) BeanFactory.getBean(Constant.FOOD_TYPE_DAO_IMPL);
    private FoodDao foodDao  = (FoodDao) BeanFactory.getBean(Constant.FOOD_DAO_IMPL);
    /**
     * 增加一个菜系
     * @param foodType
     * @return
     */
    @Override
    public int addFoodType(FoodType foodType) {

        FoodType foodType1 = null;
        try {
            foodType1 = foodTypeDao.selectFoodTypeByNameOne(foodType.getName());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (foodType1 !=null){
            return 1;
        }
        JdbcUtils.startTransaction();

        int i = 0;
        try {
            i = foodTypeDao.addFoodType(foodType);
        } catch (SQLException e) {
            e.printStackTrace();
            JdbcUtils.rollBack();
            throw  new RuntimeException("服务器出错");
        }finally {
            JdbcUtils.commit();
        }
        return i;
    }

    /**
     * 查找所有菜系
     * @return
     * @throws SQLException
     */
    @Override
    public List<FoodType> selectAllFoodType() throws SQLException {
        List<FoodType> foodTypes = foodTypeDao.selectAllFoodType();
        return foodTypes;
    }

    /**
     * 更改菜系的名字
     * @param name
     * @param id
     * @return
     */
    @Override
    public int updateFoodType(String name, int id) {
        Date date = new Date();
        JdbcUtils.startTransaction();
        int i = 0;
        try {
            i = foodTypeDao.updateFoodType(name, id, date);
        } catch (SQLException e) {
            e.printStackTrace();
            JdbcUtils.rollBack();
            throw  new RuntimeException("服务器出错");
        }finally {
            JdbcUtils.commit();
        }
        return i;
    }

    /**
     * 根据id删除一个菜系
     * @param id
     * @return
     */
    @Override
    public int deleteFoodTypeById(int id) throws SQLException {
        List<Food> foods = foodDao.selectFoodBy(id);
        if (foods.size()!=0 ){
            return 2;
        }
        JdbcUtils.startTransaction();
        int i = 0;
        try {
            i = foodTypeDao.deleteFoodTypeById(id);
        } catch (SQLException e) {
            e.printStackTrace();
            JdbcUtils.rollBack();
            throw  new RuntimeException("服务器出错");
        }finally {
            JdbcUtils.commit();
        }
        return i;
    }

    /**
     * 模糊查询菜系
     * @param name
     * @return
     * @throws SQLException
     */
    @Override
    public List<FoodType> selectFoodTypeByName(String name) throws SQLException {
        return foodTypeDao.selectFoodTypeByName(name);
    }

    /**
     * 根据id查找菜系
     * @param id
     * @return
     */
    @Override
    public FoodType selectFoodTypeById(int id) throws SQLException {
        return foodTypeDao.selectFoodTypeById(id);
    }

    /**
     * 根据菜的名字查询一个菜系
     * @param name
     * @return
     */
    @Override
    public FoodType selectFoodTypeByNameOne(String name) throws SQLException {
        return foodTypeDao.selectFoodTypeByNameOne(name);
    }

    @Test
    public void test () throws SQLException {
        FoodTypeServiceImpl foodTypeService = new FoodTypeServiceImpl();
        FoodType foodType = foodTypeService.selectFoodTypeById(4);
        System.out.println(foodType);
    }
}
